package com.jy.adscm.operation.base.controller;

import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.DownLoadReq;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zyl
 * @Date 2020/1/7 -9:23
 */
@RestController
@RequestMapping("/adscm/operation/")
public class UploadController {
        @Value("${ly.upload.imagePath}")
        private  String imagePath;
        @Value("${ly.upload.imageUrl}")
        private  String imageUrl;
        @Autowired
        RedisTemplate<Object, Token> tokenRedisTemplate;
        @RequestMapping("upload")
        public SimpleCoreResponse upload(HttpServletRequest request) throws AdscmSystemException {
            String tokenId = request.getParameter("tokenId");
            Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
            List<String> paths = null;
            CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                    request.getSession().getServletContext());
            if(multipartResolver.isMultipart(request)){
                //将request变成多部分request
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
                String yearMonth= new SimpleDateFormat("yyMM").format(new Date());
                paths = AdscmSystemUtils.getInstance().uploadFileNoNewName(multiRequest, imagePath+ yearMonth+ File.separator+ UUID.randomUUID().toString().replace("-","")+ File.separator);
            }
            String result="";
            if (paths!=null){
                result = paths.get(0);
                result=   result.replace(imagePath,"");
                result=imageUrl+result;
            }
            return new SimpleCoreResponse("200","上传成功",result);
        }
        @RequestMapping("delete")
        public SimpleCoreResponse upload(String path, HttpServletRequest request) throws AdscmSystemException {
            path=imagePath+path;
            File file =new File(path);
            if (file.exists()){
                file.delete();
            }
            return new SimpleCoreResponse("200","删除成功",true);
        }
        @RequestMapping("download")
        public SimpleCoreResponse download(@RequestBody DownLoadReq downLoadReq, HttpServletResponse resp) throws AdscmSystemException {
            String path = downLoadReq.getPath();
            path=imagePath+path.replace(imageUrl,"");
            resp.setHeader("Content-Disposition","attachment;filename=kpi.xlsx");
            resp.setHeader("Content-Type","application/octet-stream");

            AdscmSystemUtils.getInstance().downloadFile(path,resp);

            return new SimpleCoreResponse("200","下载成功",true);
        }

}
